package py.org.atom.seam.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;

import py.org.atom.seam.util.Sys;


@MappedSuperclass
public class InsUpdDisAudit extends Base{

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = Sys.INSERT_DATE_COLUMN, length = 29, nullable=false)	
	private Date insdate;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = Sys.UPDATE_DATE_COLUMN, length = 29)	
	private Date upddate;
	
	@Column(name = Sys.INSERT_USER_COLUMN, length = Sys.ID_MAXLEN, nullable=false)
	private String insusr;
	
	@Column(name = Sys.UPDATE_USER_COLUMN, length = Sys.ID_MAXLEN)
	private String updusr;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = Sys.DISABLE_DATE_COLUMN, length = 29)	
	private Date disdate;
	
	@Column(name = Sys.DISABLE_USER_COLUMN, length = Sys.ID_MAXLEN)
	private String disusr;
	
	public Date getInsdate() {
		return insdate;
	}
	public void setInsdate(Date insdate) {
		this.insdate = insdate;
	}
	public Date getUpddate() {
		return upddate;
	}
	public void setUpddate(Date upddate) {
		this.upddate = upddate;
	}
	public String getInsusr() {
		return insusr;
	}
	public void setInsusr(String insusr) {
		this.insusr = insusr;
	}
	public String getUpdusr() {
		return updusr;
	}
	public void setUpdusr(String updusr) {
		this.updusr = updusr;
	}
	public Date getDisdate() {
		return disdate;
	}
	public void setDisdate(Date disdate) {
		this.disdate = disdate;
	}
	public String getDisusr() {
		return disusr;
	}
	public void setDisusr(String disusr) {
		this.disusr = disusr;
	}
	
	@PrePersist
	public void beforePersist(){
		
		String usr = this.getUsr();
		
		if(usr != null){
			this.setInsusr(usr.trim());
			this.setInsdate(new Date());
		}
	}
	
	@PreUpdate
	public void beforeUpdate(){	
		
		String usr = this.getUsr();
		
		if(usr != null){
			this.setUpdusr(usr.trim());
			this.setUpddate(new Date());
		}
	}
	
	@Transient
	private String getUsr(){
		if(this.getIdentity() != null 
				&& this.getIdentity().getCredentials() != null
				&& this.getIdentity().getCredentials().getUsername() != null)
			return this.getIdentity().getCredentials().getUsername().trim();
		else
			return null;
	}
}
